<?php
namespace App\Repositories;

use App\Services\Tools\CommonService;
use App\Services\Tools\MqttService;
use Illuminate\Support\Facades\Redis;

class MqttRepository
{
    const PUSH_POPULARITY             = 5001;//推送房间-人气变化
    const PUSH_RIDE                   = 5003;//推送房间-坐骑进场特效
    const PUSH_NOBILITY               = 5004;//推送房间-爵位用户进场特效
    const PUSH_APPLY_COUNT            = 5005;//推送房间-上麦申请人数变化
    const PUSH_BANNED_USER            = 5007;//推送房间-用户禁言 1禁言2解禁
    const PUSH_CLOSE_PIT              = 5011;//推送房间-是否封麦 1封麦2解封
    const PUSH_CLEAR_CARDIAC          = 5013;//推送房间-清空单个麦位心动值
    const PUSH_CLEAR_CARDIAC_ALL      = 5014;//推送房间-清空所有麦位心动值
    const PUSH_SET_MANAGER            = 5015;//推送房间-设置房间管理员
    const PUSH_DELETE_MANAGER         = 5016;//推送房间-删除房间管理员
    const PUSH_SWITCH_VOICE           = 5017;//推送房间-开关麦 1开0关
    const PUSH_GIFT_BANNER            = 5019;//推送所有人-横幅礼物通知
    const PUSH_GIFT_CHAT_ROOM         = 5020;//推送房间-聊天室礼物通知
    const PUSH_FISH                   = 5021;//推送所有人-小猫钓鱼钓到大礼物时通知
    const PUSH_ROOM_PASSWORD          = 5022;//推送房间-房间密码变化通知 0取消密码1设置或修改密码
    const PUSH_CARDIAC_SWITCH         = 5023;//推送房间-房间心动值开关变化通知 1开2关
    const PUSH_ROOM_WHEAT             = 5024;//推送房间-上麦模式变化通知 1自由2排麦
    const PUSH_UPDATE_ROOM_NAME       = 5025;//推送房间-修改房间名称
    const PUSH_WEEK_STAR              = 5027;//推送房间-周星用户进场特效
    const PUSH_UPDATE_ROOM_BACKGROUND = 5028;//推送房间-修改房间背景
    const PUSH_UPDATE_ROOM_PLAYING    = 5029;//推送房间-修改房间 玩法|公告
    const PUSH_BOSS_ATTACK            = 5030;//推送房间-BOSS大作战 超过99999金币 发送所有人消息
    const PUSH_BOSS_BLOOD             = 5031;//推送所有正在攻击boss的用户-boss大作战血量变化推送
    const PUSH_PIT_ON                 = 5032;//推送房间-上麦
    const PUSH_PIT_DOWN               = 5033;//推送房间-下麦
    const PUSH_KICK_OUT               = 5034;//推送单独用户-被踢出房间
    const PUSH_APPLY_USER             = 5035;//推送单独用户-定向推向给上麦的用户
    const PUSH_SHUT_UP                = 5036;//推送房间-用户禁麦 1禁麦2解禁
    const PUSH_JOIN_ROOM              = 5037;//推送房间-用户进入房间
    const PUSH_COUNT_DOWN             = 5038;//推送房间-麦位倒计时
    const PUSH_ROLL                   = 5039;//推送房间-扔骰子
    const PUSH_FM_GOLD                = 5040;//推送所有房间-电台房开通黄金守护
    const PUSH_FACE                   = 5041;//推送房间-在麦上发送表情
    const PUSH_ZEGO_LOG               = 5042;//推送单独用户-上传即构日志
    const PUSH_ROOM_CHAT_STATUS       = 5043;//推送房间-公屏状态
    const PUSH_BALL_START             = 5044;//推送房间-球球大作战-开球
    const PUSH_BALL_THROW             = 5045;//推送房间-球球大作战-弃球
    const PUSH_BALL_SHOW              = 5046;//推送房间-球球大作战-亮球
    const PUSH_SOUND_EFFECT_CHANGE    = 5047;//推送房间-房间音效改变
    const PUSH_ORDER_RECEIVE          = 5048;//推送大神用户-有用户下单
    const PUSH_ORDER_REFUND           = 5049;//推送给大神-有用户要退款
    const PUSH_ROOM_DEMAND_BOSS       = 5050;//推送单独用户-推送给8号麦的老板
    const PUSH_ROOM_DEMAND_UPDATE     = 5051;//推送房间-更新派单需求

    public $user_id, $room_id, $topic_room, $topic_client;

    public function __construct($user_id = 0, $room_id = 0)
    {
        $this->user_id = $user_id;
        $this->room_id = $room_id;

        $this->topic_room = 'room_' . $this->room_id;
        $this->topic_client = 'user_' . $this->user_id;
    }

    public function setUser($user_id)
    {
        $this->user_id = $user_id;
        $this->topic_client = 'user_' . $user_id;
    }

    public function setRoom($room_id)
    {
        $this->room_id = $room_id;
        $this->topic_room = 'room_' . $room_id;
    }

    private function push($push_type, $topic, $data = [])
    {
        $mqtt = new MqttService();
        $content = json_encode(
            [
                'type' => $push_type,
                'time' => CommonService::getMillisecond(),
                'msg'  => $data,
            ]
        );
        $mqtt->publish($topic, $content);
    }

    //推送人气
    public function updatePopularity($popularity)
    {
        $topic = '$delayed/1/' . $this->topic_room;
        $data = ['popularity' => $popularity, 'room_id' => $this->room_id];
        $this->push(self::PUSH_POPULARITY, $topic, $data);
    }

    //推送上麦
    public function pitOn($pit_number, $data)
    {
        $topic = $this->topic_room;
        $data['room_id'] = $this->room_id;
        $data['pit_number'] = $pit_number;

        $ball = Redis::set('room:user:ball:' . $this->room_id . ':' . $this->user_id);
        $data['ball_state'] = $ball === false ? 0 : 1;
        $this->push(self::PUSH_PIT_ON, $topic, $data);
    }

    //推送下麦
    public function pitDown($pit_number)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number, 'user_id' => $this->user_id];
        $this->push(self::PUSH_PIT_DOWN, $topic, $data);
    }

    //带坐骑用户进场特效通知
    public function ride($ride_info)
    {
        $topic = '$delayed/1/' . $this->topic_room;
        $data = [
            'room_id'   => $this->room_id,
            'ride_url'  => $ride_info['special'],
            'show_type' => $ride_info['show_type']
        ];
        $this->push(self::PUSH_RIDE, $topic, $data);
    }

    //带爵位用户进场特效通知
    public function nobility($data)
    {
        $topic = '$delayed/1/' . $this->topic_room;
        if ($data['nobilityId'] == 6) {
            $special = 'https://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/nobility/' . $data['nobilityId'] . '.svga';
        } else {
            $special = '';
        }

        //爵位特效暂时不推
        $special = '';

        $data = [
            'room_id'       => $this->room_id,
            'user_id'       => $this->user_id,
            'nobility_name' => $data['nobilityName'],
            'nobility_id'   => $data['nobilityId'],
            'nobility_icon' => $data['nobility_icon'],
            'avatar'        => $data['headPicture'],
            'nickname'      => $data['userName'],
            'special'       => $special,
            'sex'           => $data['sex'],
        ];
        $this->push(self::PUSH_NOBILITY, $topic, $data);
    }

    //上麦申请人数变化通知
    public function applyCount($count, $user_ids = '')
    {
        $topic = $this->topic_room;

        $count_8 = M('RoomPitApply')->where(['room_id' => $this->room_id, 'pit_number' => 8])->count();
        $count_8 = $count_8 > 0 ? $count_8 : 0;

        $total_count = $count - $count_8;
        $data = [
            'room_id'  => $this->room_id,
            'count'    => $total_count > 0 ? $total_count : 0,
            'count_8'  => $count_8,
            'user_ids' => $user_ids
        ];
        $this->push(self::PUSH_APPLY_COUNT, $topic, $data);
    }

    //用户被禁言 action 1禁言2解禁
    public function bannedUser($action)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'user_id' => $this->user_id, 'action' => $action];
        $this->push(self::PUSH_BANNED_USER, $topic, $data);
    }

    //是否封麦 1.封麦 2.解除封麦
    public function closePit($pit_number, $action)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number, 'action' => $action];
        $this->push(self::PUSH_CLOSE_PIT, $topic, $data);
    }

    //清空单个麦位心动值
    public function clearCardiac($pit_number)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number];
        $this->push(self::PUSH_CLEAR_CARDIAC, $topic, $data);
    }

    //清空所有麦位心动值
    public function clearCardiacAll()
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id];
        $this->push(self::PUSH_CLEAR_CARDIAC_ALL, $topic, $data);
    }

    //设置房间管理员
    public function setManager()
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'user_id' => $this->user_id];
        $this->push(self::PUSH_SET_MANAGER, $topic, $data);
    }

    //删除房间管理员
    public function deleteManager()
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'user_id' => $this->user_id];
        $this->push(self::PUSH_DELETE_MANAGER, $topic, $data);
    }

    //开关麦 action 1开0关
    public function switchVoice($action, $pit_number = 0)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id'    => $this->room_id,
            'user_id'    => $this->user_id,
            'pit_number' => $pit_number,
            'action'     => $action
        ];
        $this->push(self::PUSH_SWITCH_VOICE, $topic, $data);
    }

    //横幅礼物通知
    public function giftBanner($gift_list)
    {
        $topic = 'room';
        $data = ['room_id' => $this->room_id, 'list' => $gift_list];
        $this->push(self::PUSH_GIFT_BANNER, $topic, $data);
    }

    //聊天室礼物通知
    public function giftChatRoom($gift_list, $cardiac, $contribution, $show_cat = 0)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id'      => $this->room_id,
            'gift_list'    => $gift_list,
            'cardiac_list' => $cardiac,
            'contribution' => $contribution,
            'show_cat'     => $show_cat,
            'user_id'      => $this->user_id
        ];
        $this->push(self::PUSH_GIFT_CHAT_ROOM, $topic, $data);
    }

    //小猫钓鱼钓到大礼物时通知
    public function fish($gift_list, $user_info)
    {
        $txt = "<font color='#FFFFFF'>哇塞</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在小猫钓鱼中获得</font>";
        $txt_extra = [];
        foreach ($gift_list as $key => $value) {
            if ($value['price'] >= 99999) {
                $txt_extra [] = [
                    'text'    => $txt . "<font color='#FABA5C'>" . $value['prize_title'] . "</font><font color='#FFFFFF'>X" . $value['number'] . "</font>",
                    'picture' => $value['picture']
                ];
                $log [] = $user_info['nickname'] . '|' . $value['prize_title'] . '|' . $value['number'];
            }
        }
        if (!empty($txt_extra)) {
            $topic = 'room';
            Log::record(var_export($log, true));
            $this->push(self::PUSH_FISH, $topic, $txt_extra);
        }
    }

    //房间密码 0取消密码1设置或修改密码
    public function roomPassword($action)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'action' => $action];
        $this->push(self::PUSH_ROOM_PASSWORD, $topic, $data);
    }

    //房间心动值开关 1开2关
    public function cardiacSwitch($action)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'action' => $action];
        $this->push(self::PUSH_CARDIAC_SWITCH, $topic, $data);
    }

    //上麦模式变化 1自由2排麦
    public function roomWheat($action)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'action' => $action];
        $this->push(self::PUSH_ROOM_WHEAT, $topic, $data);
    }

    //修改房间名称
    public function updateRoomName($room_name)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'room_name' => $room_name];
        $this->push(self::PUSH_UPDATE_ROOM_NAME, $topic, $data);
    }

    //周星用户进场
    public function weekStar($nickname, $rank)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'nickname' => $nickname, 'rank' => $rank];
        $this->push(self::PUSH_WEEK_STAR, $topic, $data);
    }

    //修改房间背景
    public function updateRoomBackground($background)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'background' => $background];
        $this->push(self::PUSH_UPDATE_ROOM_BACKGROUND, $topic, $data);
    }

    //修改房间玩法|公告
    public function updateRoomPlaying($playing, $greeting)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'playing' => $playing, 'greeting' => $greeting];
        $this->push(self::PUSH_UPDATE_ROOM_PLAYING, $topic, $data);
    }

    //boss大作战 超过99999金币 发送所有人消息

    public function AttackBoss($gift_list, $user_info)
    {
        $txt = "<font color='#FFFFFF'>哇塞</font><font color='#FD8469'>" . $user_info['nickname'] . "</font><font color='#FFFFFF'>在BOSS大作战中获得</font>";
        $txt_extra = [];
        foreach ($gift_list as $key => $value) {
            if ($value['price'] >= 52000) {
                $txt_extra [] = [
                    'text'    => $txt . "<font color='#FABA5C'>" . $value['prize_title'] . "</font><font color='#FFFFFF'>X" . $value['number'] . "</font>",
                    'picture' => $value['picture']
                ];
            }
        }
        if (!empty($txt_extra)) {
            $topic = 'room';
            $this->push(self::PUSH_BOSS_ATTACK, $topic, $txt_extra);
        }
    }

    //boss大作战血量变化推送
    public function bossBlood($left_blood = 0, $total_blood = 0, $gift_list = [], $user_info = [])
    {
        if ($gift_list) {
            foreach ($gift_list as $key => $value) {
                if ($value['price'] >= 5200) {
                    $gift_list[$key]['nickname'] = $user_info['nickname'];
                } else {
                    unset($gift_list[$key]);
                }
            }
        }
        $topic = 'boss';
        $data = ['left_blood' => $left_blood, 'total_blood' => $total_blood, 'gift_list' => array_values($gift_list)];
        $this->push(self::PUSH_BOSS_BLOOD, $topic, $data);
    }

    //定向推送给上麦的用户
    public function agreeApplyUser($pit_number)
    {
        $topic = $this->topic_client;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number];
        $this->push(self::PUSH_APPLY_USER, $topic, $data);
    }

    //定向推送给被踢出房间的用户
    public function kickOut()
    {
        $topic = $this->topic_client;
        $data = ['room_id' => $this->room_id, 'user_id' => $this->user_id];
        $this->push(self::PUSH_KICK_OUT, $topic, $data);
    }

    //用户禁麦 action 1禁麦2解禁
    public function shutUp($action, $pit_number)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id'    => $this->room_id,
            'action'     => $action,
            'pit_number' => $pit_number,
            'user_id'    => $this->user_id
        ];
        $this->push(self::PUSH_SHUT_UP, $topic, $data);
    }

    //用户进入房间
    public function joinRoom($nickname, $rank_icon, $role, $user_is_new)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id'     => $this->room_id,
            'user_id'     => $this->user_id,
            'nickname'    => $nickname,
            'rank_icon'   => $rank_icon,
            'role'        => $role,
            'user_is_new' => $user_is_new
        ];
        $this->push(self::PUSH_JOIN_ROOM, $topic, $data);
    }

    //麦位倒计时
    public function countDown($pit_number, $seconds)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number, 'seconds' => $seconds];
        $this->push(self::PUSH_COUNT_DOWN, $topic, $data);
    }

    //扔骰子
    public function roll($number, $pit_number)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id'    => $this->room_id,
            'user_id'    => $this->user_id,
            'number'     => $number,
            'pit_number' => $pit_number
        ];
        $this->push(self::PUSH_ROLL, $topic, $data);
    }

    //电台房开通黄金守护
    public function fmGold($nickname_from, $nickname_to)
    {
        $topic = 'room';
        $data = ['room_id' => $this->room_id, 'nickname_from' => $nickname_from, 'nickname_to' => $nickname_to];
        $this->push(self::PUSH_FM_GOLD, $topic, $data);
    }

    //发送表情
    public function face($pit_number, $picture, $special)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id'    => $this->room_id,
            'pit_number' => $pit_number,
            'picture'    => $picture,
            'special'    => $special
        ];
        $this->push(self::PUSH_FACE, $topic, $data);
    }

    //上传即构日志
    public function zegoLog()
    {
        $topic = $this->topic_client;
        $data = ['user_id' => $this->user_id];
        $this->push(self::PUSH_ZEGO_LOG, $topic, $data);
    }

    //公屏状态
    public function roomChatStatus($status)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'status' => $status];
        $this->push(self::PUSH_ROOM_CHAT_STATUS, $topic, $data);
    }

    //球球大作战-开球
    public function ballStart($pit_number)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number];
        $this->push(self::PUSH_BALL_START, $topic, $data);
    }

    //球球大作战-弃球
    public function ballThrow($pit_number)
    {
        $topic = $this->topic_room;
        $data = ['room_id' => $this->room_id, 'pit_number' => $pit_number];
        $this->push(self::PUSH_BALL_THROW, $topic, $data);
    }

    //球球大作战-亮球
    public function ballShow($pit_number, $first, $second, $third)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id'    => $this->room_id,
            'pit_number' => $pit_number,
            'first'      => $first,
            'second'     => $second,
            'third'      => $third,
            'user_id'    => $this->user_id
        ];
        $this->push(self::PUSH_BALL_SHOW, $topic, $data);
    }

    //房间音效改变
    public function soundEffectChange($sound_effect_detail)
    {
        $topic = $this->topic_room;
        $data = [
            'room_id'         => $this->room_id,
            'id'              => $sound_effect_detail['id'],
            'config'          => $sound_effect_detail['config'],
        ];
        $this->push(self::PUSH_SOUND_EFFECT_CHANGE, $topic, $data);
    }
}

